const express = require('express');
const router = express.Router();
const Prize = require('../../models/Prizes/Prizes');
const multer = require('multer');
const path = require('path');


// 添加奖品
// multer配置，存储到本地uploads文件夹
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'uploads/'); // 请确保此目录存在，或者做相应创建
  },
  filename: (req, file, cb) => {
    // 用时间戳+原扩展名避免重名
    cb(null, Date.now() + path.extname(file.originalname));
  }
});

const upload = multer({ storage });

// 单文件上传字段名叫 prize_image
router.post('/addPrize', upload.single('prize_image'), async (req, res) => {
  try {
    // 文件上传后multer会把文件信息放到 req.file
    if (!req.file) {
      return res.status(400).json({ message: '奖品图片必须上传' });
    }

    // 其他字段在 req.body 里
    const { prize_name, prize_description, points, stock } = req.body;

    if (!prize_name || points == null || stock == null) {
      return res.status(400).json({ message: '缺少必要字段' });
    }

    // 图片路径，假设你的静态资源路径配置是 /uploads
    const prize_image_url = `/uploads/${req.file.filename}`;

    const prize = await Prize.create({
      prize_image: prize_image_url,
      prize_name,
      prize_description,
      points: parseInt(points, 10),
      stock: parseInt(stock, 10),
      created_at: new Date(),
      updated_at: new Date()
    });

    res.status(201).json(prize);
  } catch (error) {
    console.error('添加奖品失败:', error);
    res.status(500).json({ message: '服务器错误' });
  }
});




// 获取所有奖品
router.get('/getAllPrizes', async (req, res) => {
  try {
    const prizes = await Prize.findAll();
    res.json(prizes);
  } catch (error) {
    console.error('获取奖品列表失败:', error);
    res.status(500).json({ message: '服务器错误' });
  }
});





// 获取单个奖品
router.get('/searchPrize/:id', async (req, res) => {
  try {
    const prize = await Prize.findByPk(req.params.id);
    if (!prize) return res.status(404).json({ message: '奖品未找到' });
    res.json(prize);
  } catch (error) {
    console.error('获取奖品失败:', error);
    res.status(500).json({ message: '服务器错误' });
  }
});

// 更新奖品
router.put('/matchPrize/:id', async (req, res) => {
  try {
    const { prize_image, prize_name, prize_description, points, stock } = req.body;
    const prize = await Prize.findByPk(req.params.id);
    if (!prize) return res.status(404).json({ message: '奖品未找到' });

    await prize.update({
      prize_image: prize_image ?? prize.prize_image,
      prize_name: prize_name ?? prize.prize_name,
      prize_description: prize_description ?? prize.prize_description,
      points: points ?? prize.points,
      stock: stock ?? prize.stock,
      updated_at: new Date()
    });

    res.json(prize);
  } catch (error) {
    console.error('更新奖品失败:', error);
    res.status(500).json({ message: '服务器错误' });
  }
});



// 根据id删除奖品
router.delete('/delPrize/:id', async (req, res) => {
  try {
    const prize = await Prize.findByPk(req.params.id);
    if (!prize) return res.status(404).json({ message: '奖品未找到' });

    await prize.destroy();
    res.json({ message: '奖品删除成功' });
  } catch (error) {
    console.error('删除奖品失败:', error);
    res.status(500).json({ message: '服务器错误' });
  }
});

module.exports = router;
